import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note:子菜单只在路由子菜单时出现。长度> = 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                  如果设置为true，则item将不会显示在侧边栏中(默认为false)
 * alwaysShow: true              如果设置为true，将始终显示根菜单
 *                               如果没有设置alwaysShow，当item有多个子route时，
 *                               它将变成嵌套模式，否则不会显示根菜单
 * redirect: noRedirect          如果设置noRedirect将不会在面包屑中重定向
 * name:'router-name'           (必须设置!!)使用该名称。
 * meta : {
    roles: ['admin','editor']   控制页面角色(您可以设置多个角色)
    title: 'title'              名称显示在侧边栏和面包屑(推荐集)
    icon: 'svg-name'/'el-icon-x'图标显示在侧边栏
    breadcrumb: false           如果设置为false，该项目将隐藏在breadcrumb中(默认为true)
    activeMenu: '/example/list' 如果设置路径，工具条将突出显示您设置的路径
  }
 */

/**
 * constantRoutes
 * 没有权限要求的基本页
 * 可以访问所有角色
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/baseConfig/orders',
    children: [{
      path: 'home',
      name: 'home',
      hidden: true,
      component: () => import('@/views/home/index'),
      meta: { title: '首页', icon: 'home' }
    }]
  }
]

/**
 * asyncRoutes
 * 需要根据用户角色动态加载的路由
 */
export const asyncRoutes = [
  {
    path: '/systemConfig',
    component: Layout,
    redirect: '/systemConfig/users',
    name: 'systemConfig',
    meta: { title: '系统配置', icon: 'system', roles: ['admin'] },
    children: [
      {
        path: 'users',
        name: 'users',
        component: () => import('@/views/users/index'),
        meta: { title: '用户管理', icon: 'users', roles: ['admin'] }
      },
      {
        path: 'agreements',
        name: 'agreements',
        component: () => import('@/views/agreements/index'),
        meta: { title: '协议管理', icon: 'agreements', roles: ['admin'] }
      },
      {
        path: 'addAgreements',
        name: 'addAgreements',
        hidden: true,
        component: () => import('@/views/agreements/add'),
        meta: { title: '添加协议', icon: 'agreements' }
      }
    ]
  },
  {
    path: '/baseConfig',
    component: Layout,
    redirect: '/baseConfig/goods',
    name: 'baseConfig',
    meta: { title: '业务数据', icon: 'baseConfig', roles: ['admin'] },
    children: [
      {
        path: 'goods',
        name: 'goods',
        component: () => import('@/views/goods/index'),
        meta: { title: '产品管理', icon: 'goods', roles: ['admin'] }
      },
      {
        path: 'orders',
        name: 'orders',
        component: () => import('@/views/orders/index'),
        meta: { title: '订单管理', icon: 'orders', roles: ['admin'] }
      }
    ]
  },
  // {
  //   path: '/logs',
  //   component: Layout,
  //   redirect: '/logs/logs',
  //   name: 'logs',
  //   meta: { title: '日志', icon: 'base', roles: ['admin'] },
  //   children: [
  //     {
  //       path: 'logs',
  //       name: 'logs',
  //       component: () => import('@/views/logs/index'),
  //       meta: { title: '日志记录', icon: 'logs', roles: ['admin'] }
  //     }
  //   ]
  // },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
